package testes.examples;


import visualcbir.format.FormatPng;
import visualcbir.format.IFormat;
import visualcbir.format.IPrintable;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.font.TextAttribute;
import java.awt.font.TextLayout;
import java.awt.geom.AffineTransform;
import java.awt.geom.Area;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import java.io.File;
import java.text.AttributedString;
import javax.swing.JComponent;

/**
 *
 * @author sheila
 */
public class FormatExample extends JComponent implements IPrintable {

    public static void main(String[] args) throws Exception {

        IFormat format = new FormatPng();
        format.printFormat(new File("viejesito2.png"), new FormatExample());
    }

    @Override
    public void printWithGraphics(Graphics g) {
        this.paint(g);
    }
        
    @Override
    //protected void paintComponent(Graphics g){
    public void paint(Graphics g){
        System.out.println("llamo al paint component adequado");
        Graphics2D g2d = (Graphics2D) g;

        //g.setClip(new Ellipse2D.Double(0, 0, 400, 400));
        //g.clipRect(100, 0, 500, 300);

        g2d.setBackground(Color.red);

        g2d.setColor(Color.decode("#ccccff"));
        g2d.setBackground(Color.white);
        g2d.setStroke(new BasicStroke(20.0f));
        g2d.setTransform(AffineTransform.getScaleInstance(1, 1));

        g2d.drawOval(0, 0, 500, 500);

        g2d.setStroke(new BasicStroke(1.0f));

        g2d.setColor(Color.gray);
        g2d.draw3DRect(5, 5, 30, 30, false);
        g2d.fill3DRect(45, 5, 30, 30, false);
        g2d.draw3DRect(85, 5, 30, 30, true);
        g2d.fill3DRect(125, 5, 30, 30, true);

        g2d.setColor(Color.black);
        g2d.drawLine(20, 60, 100, 70);

        g2d.fillRect(20, 80, 30, 30);
        g2d.drawRect(40, 90, 30, 30);

        g2d.clearRect(20, 80, 10, 10);

        g2d.drawRoundRect(20, 130, 40, 40, 5, 5);
        g2d.fillRoundRect(80, 130, 40, 40, 5, 5);

        g2d.drawOval(20, 180, 30, 20);
        g2d.fillOval(60, 180, 30, 20);

        g2d.drawArc(20, 220, 40, 40, 0, 315);
        g2d.fillArc(80, 220, 40, 40, 0, 315);

        g2d.drawPolyline(new int[]{20, 40, 60, 80}, new int[]{280, 300, 290, 310}, 4);

        g2d.drawPolygon(new int[]{20, 40, 60, 50}, new int[]{340, 320, 340, 350}, 4);

        g2d.fillPolygon(new int[]{80, 100, 120, 110}, new int[]{340, 320, 340, 350}, 4);

        g2d.drawChars("toCharArray()".toCharArray(), 0, 13, 20, 390);
        g2d.drawBytes("getBytes()".getBytes(), 0, 10, 20, 430);


        // A more advanced test with a Venn 2
        Ellipse2D.Float j = new Ellipse2D.Float(200, 20, 100, 80);
        Ellipse2D.Float k = new Ellipse2D.Float(250, 20, 100, 80);
        Area jk = new Area(j);
        jk.intersect(new Area(k));
        g2d.setColor(Color.decode("#ffcccc"));
        g2d.fill(j);
        g2d.setColor(Color.decode("#ccffcc"));
        g2d.fill(k);
        g2d.setColor(Color.decode("#ffffcc"));
        g2d.fill(jk);
        g2d.setColor(Color.black);
        g2d.setStroke(new BasicStroke(2.0f));
        g2d.draw(j);
        g2d.draw(k);

        // Venn 3 unshaded
        Area a = new Area(new Ellipse2D.Float(200, 160, 100, 100));
        Area b = new Area(new Ellipse2D.Float(250, 160, 100, 100));
        Area c = new Area(new Ellipse2D.Float(225, 116.7f, 100, 100));

        Area shaded = new Area(b);
        shaded.intersect(c);
        shaded.subtract(a);
        g2d.setColor(Color.decode("#ccccff"));
        g2d.fill(shaded);

        g2d.setColor(Color.black);
        g2d.setStroke(new BasicStroke(2.0f));
        g2d.draw(a);
        g2d.draw(b);
        g2d.draw(c);

        // Venn 3 shaded
        a = new Area(new Ellipse2D.Float(200, 340, 100, 100));
        b = new Area(new Ellipse2D.Float(250, 340, 100, 100));
        c = new Area(new Ellipse2D.Float(225, 296.7f, 100, 100));

        g2d.setColor(Color.decode("#ffcccc"));
        g2d.fill(a);
        g2d.setColor(Color.decode("#ccffcc"));
        g2d.fill(b);
        g2d.setColor(Color.decode("#ccccff"));
        g2d.fill(c);

        Area ab = new Area(a);
        ab.intersect(b);
        g2d.setColor(Color.decode("#ffffcc"));
        g2d.fill(ab);

        Area ac = new Area(a);
        ac.intersect(c);
        g2d.setColor(Color.decode("#ffccff"));
        g2d.fill(ac);

        Area bc = new Area(b);
        bc.intersect(c);
        g2d.setColor(Color.decode("#ccffff"));
        g2d.fill(bc);

        Area abc = new Area(a);
        abc.intersect(b);
        abc.intersect(c);
        g2d.setColor(Color.decode("#ffffff"));
        g2d.fill(abc);


        g2d.setColor(Color.black);
        g2d.setStroke(new BasicStroke(1.0f));
        g2d.draw(a);
        g2d.draw(b);
        g2d.draw(c);

        // Draw some normal text.
        g2d.setFont(new Font("Serif", Font.ITALIC, 16));
        g2d.drawString("Hello, world!", 20, 50);

        // Draw some outlined text.
        AttributedString string = new AttributedString("EpsGraphics2D");
        string.addAttribute(TextAttribute.FONT, new Font("SansSerif", Font.PLAIN, 48));
        TextLayout layout = new TextLayout(string.getIterator(), g2d.getFontRenderContext());
        Shape textShape = layout.getOutline(AffineTransform.getTranslateInstance(100, 120));
        g2d.setStroke(new BasicStroke(1.0f));
        g2d.setColor(Color.decode("#ccccff"));
        g2d.fill(textShape);
        g2d.setColor(Color.black);
        g2d.draw(textShape);

        // Draw dashed rectangle around the outlined text.
        Rectangle2D boundingRect = textShape.getBounds2D();
        int curve = 10;
        RoundRectangle2D roundRect = new RoundRectangle2D.Double(boundingRect.getX() - curve, boundingRect.getY() - curve, boundingRect.getWidth() + curve * 2, boundingRect.getHeight() + curve * 2, curve, curve);
        g2d.setColor(Color.blue);
        g2d.setStroke(new BasicStroke(2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL, 1.0f, new float[]{10, 5}, 0));
        g2d.draw(roundRect);

        g2d.rotate(Math.PI / 2, 300, 300);
        g2d.drawString("Vertical Text", 300, 300);

    }

}
